Skip to content

Remove dependency on async library#555

Merged
RubenVerborgh merged 5 commits into
dz_oidcfrom
fix/remove-async
Aug 22, 2017
Merged

Remove dependency on async library#555
RubenVerborgh merged 5 commits into
dz_oidcfrom
fix/remove-async

Conversation

@RubenVerborgh

Copy link
Copy Markdown
Contributor

No description provided.

@RubenVerborgh RubenVerborgh removed their assignment Aug 21, 2017
@RubenVerborgh RubenVerborgh added this to the 4.0.0 milestone Aug 21, 2017
@RubenVerborgh RubenVerborgh requested a review from dan-f August 21, 2017 19:32
Comment thread lib/handlers/allow.js
var ACL = require('../acl-checker')
var $rdf = require('rdflib')
var url = require('url')
var async = require('async')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

Comment thread lib/handlers/allow.js
readFile(uri, host, ldp, baseUri).then(body => {
const graph = $rdf.graph()
$rdf.parse(body, graph, uri, 'text/turtle')
return graph

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly unrelated to this PR, but these 3 lines come up again and again when working with rdflib.js. I'd love to have an alternative API which takes a string and returns a graph.

Comment thread lib/handlers/get.js
async.each(matches, function (match, done) {
Promise.all(matches.map(match => new Promise((resolve, reject) => {
var baseUri = utils.filenameToBaseUri(match, reqOrigin, root)
fs.readFile(match, {encoding: 'utf8'}, function (err, fileData) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again probably not in scope for the PR, but I'm confused as to why we need a readFile declared here vs fs.readFile.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, unsure. I just ported it.

Comment thread lib/ldp.js
var stringToStream = require('./utils').stringToStream
var serialize = require('./utils').serialize
var extend = require('extend')
var doWhilst = require('async').doWhilst

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doWhilst?? haha!

Comment thread lib/ldp.js
// add container stats
new Promise((resolve, reject) =>
ldpContainer.addContainerStats(ldp, reqUri, filename, resourceGraph,
err => err ? reject(err) : resolve())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nitpick comment (don't need to address) but the indentation here through me off as to which expression the anonymous function was being passed off to.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can address this, as there are some other points as well. What indentation would you use here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely a matter of taste but this is how I generally like things: when breaking function arguments up into several lines, I like to keep the closing paren ) on its own line, indented to match the start of the line of the function call.

For example:

ldpContainer.addContainerStats(
  ldp, reqUri, filename, resourceGraph,
  err => err ? reject(err) : resolve()
)

I also see this a lot when using callback functions - breaking the line after the arguments to the callback:

ldpContainer.addContainerStats(ldp, reqUri, filename, resourceGraph, err =>
  err ? reject(err) : resolve()
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really consider using a linter that goes beyond standard. There are still lots of variations in the code.

This particular instance can perhaps be solved with promisify (see below).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, fair point. A code formatter would be nice. I thought I saw you discussing prettier at some point in gitter. I'd be in favor of adding that, for no other reason than not having to think about style ever again :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't have been me 😄 I'm in favor of a strict .eslintrc (and really dislike standard 😉).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaving this as-is, since other parts in the same file follow the same indentation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine. Consistency is more important :)

Comment thread lib/ldp.js
// read directory
.then(() => new Promise((resolve, reject) =>
ldpContainer.readdir(filename,
(err, files) => err ? reject(err) : resolve(files))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a node convention for pre-Promise API async functions to take callbacks which themselves take a possible error as the first argument and a successful value as the next argument. We could write a higher-order function to make these kinds of functions return promises:

const promisify = (fn) =>
  (...args) =>
    new Promise((resolve, reject) =>
      fn(...args, (err, success) =>
        err
          ? reject(err)
          : resolve(success)
      )
    )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that—and there are a couple of libraries that expose such a function (starting with Node 8 even in core). I've used that in other projects.

I was hesitant to do this here and in other places of the Solid code, because callbacks are sometimes used inconsistently (they don't always have an error argument). So I could start using it here, but then it would perhaps be good to keep an eye on it in other places of the code as well. And eventually, we should migrate to an await/async style, I suppose.

So, what do you think, should we start the promisify practice here?
Perhaps we should consider switching to async/await (with build step) for node-solid-server v5? Or already start it here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points!

I was hesitant to do this here and in other places of the Solid code, because callbacks are sometimes used inconsistently (they don't always have an error argument).

Oof yeah that complicates things!

And eventually, we should migrate to an await/async style, I suppose.

Sure thing. Though async/await is syntactic sugar on top of Promises, so I think that "promisifying" our interfaces goes hand-in-hand with async/await-ifying them :)

I like your point that this change is more of an interface redesign than simply "promisifying" the existing APIs. How about we simply leave things as they are here, and I'll create a ticket for making the async APIs more consistent.

@RubenVerborgh RubenVerborgh Aug 22, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll make the other changes and merge.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(created issue #557)

Comment thread test/integration/cors-proxy-test.js Outdated
nock('https://example.org').get('/404').reply(404)
nock('https://example.org').get('/401').reply(401)
nock('https://example.org').get('/500').reply(500)
nock('https://example.org').get('/200').reply(200)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace these four lines with:

nock('https://example.org')
  .get('/404').reply(404)
  .get('/401').reply(401)
  .get('/500').reply(500)
  .get('/200').reply(200) 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will do.

@dan-f

dan-f commented Aug 22, 2017

Copy link
Copy Markdown
Contributor

Just a few suggestions, but no blockers from me. 👍, nice work!

@RubenVerborgh RubenVerborgh merged commit d54f958 into dz_oidc Aug 22, 2017
@RubenVerborgh RubenVerborgh deleted the fix/remove-async branch August 22, 2017 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants